package com.hex.ds.hdtp.core.app.data.transfer.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.hex.ds.hdtp.core.app.common.page.PageData;
import com.hex.ds.hdtp.core.app.common.page.converter.IPageAndPageDtoConverter;
import com.hex.ds.hdtp.core.app.common.page.dto.PageDto;
import com.hex.ds.hdtp.core.app.data.transfer.converter.IDataTransferLogConverter;
import com.hex.ds.hdtp.core.app.data.transfer.dto.request.DataTransferDownwardNumDto;
import com.hex.ds.hdtp.core.app.data.transfer.dto.request.DataTransferLogRequestDto;
import com.hex.ds.hdtp.core.app.data.transfer.dto.response.DataTransferLogResponseDto;
import com.hex.ds.hdtp.core.app.data.transfer.entry.DownNumInfo;
import com.hex.ds.hdtp.core.app.data.transfer.service.IDataTransferLogService;
import com.hex.ds.hdtp.core.app.metadata.service.ISourceInfoService;
import com.hex.ds.hdtp.core.inf.common.page.Page;
import com.hex.ds.hdtp.core.inf.data.transfer.po.DataTransferLogPo;
import com.hex.ds.hdtp.core.inf.data.transfer.repository.IDataTransferLogRepository;
import com.hex.ds.hdtp.core.inf.data.transfer.repository.IDataTransferTaskRepository;
import com.hex.ds.hdtp.core.inf.metadata.po.SourceInfoPo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import javax.jdo.annotations.Transactional;
import java.util.List;

/**
 * @Package: com.hex.ds.hdtp.core.app.data.transfer.service.impl
 * @ClassName DataTransferLogService
 * @Description: 数据迁移记录-业务接口实现
 * @Author: wz.li
 * @Date 2023/8/18 13:48
 * @Version v2.0.2
 **/
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class DataTransferLogService implements IDataTransferLogService {

    private final IDataTransferLogRepository dataTransferLogRepository;

    private final IDataTransferLogConverter converter;

    private final IDataTransferTaskRepository dataTransferTaskRepository;

    private final IPageAndPageDtoConverter pageAndPageDtoConverter;

    private final ISourceInfoService sourceInfoService;

    /*
     * @Method: queryListByPage <br>
     * @Param: [dataTransferLogRequestDto, pageDto] <br>
     * @Return: com.hex.ds.hdtp.core.app.common.page.PageData <br>
     * @Description：分页查询<br>
     * @Author： wz.li<br>
     * @Date： 2023/8/18 13:49 <br>
     * @Version： V2.0.2<br>
     */
    @Override
    public PageData queryListByPage(DataTransferLogRequestDto dataTransferLogRequestDto, PageDto pageDto) {
        final Page<DataTransferLogPo> pageResult = dataTransferLogRepository.queryListByPage(
                converter.toPo(dataTransferLogRequestDto), pageAndPageDtoConverter.toPage(pageDto));
        List<DataTransferLogResponseDto> responseDtos = converter.toDtoList(pageResult.getData());
        for (DataTransferLogResponseDto dataTransferLog : responseDtos) {
            dataTransferLog.setSourceConnName(getSourceConnName(dataTransferLog.getSourceConnPkId()));
            if (!StrUtil.isEmpty(dataTransferLog.getTargetConnPkId())) {
                dataTransferLog.setTargetConnName(getSourceConnName(dataTransferLog.getTargetConnPkId()));
            }
        }
        return new PageData(responseDtos, pageResult.getTotal());
    }

    /*
     * @Method: getSourceConnName <br>
     * @Param: [sourceConnPkId] <br>
     * @Return: java.lang.String <br>
     * @Description：根据主键获取服务器名称<br>
     * @Author： wz.li<br>
     * @Date： 2023/8/25 17:22 <br>
     * @Version： V2.0.2<br>
     */
    private String getSourceConnName(String sourceConnPkId){
        final SourceInfoPo sourceInfoPo = sourceInfoService.queryByPkId(sourceConnPkId);
        if (sourceInfoPo != null){
            return sourceInfoPo.getSourceName();
        }
        return null;
    }

    /*
     * @Method: add <br>
     * @Param: [dataTransferLogRequestDto] <br>
     * @Return: void <br>
     * @Description：新增<br>
     * @Author： wz.li<br>
     * @Date： 2023/8/18 13:51 <br>
     * @Version： V2.0.2<br>
     */
    @Override
    public String add(DataTransferLogRequestDto dataTransferLogRequestDto) {
        DataTransferLogPo dataTransferLogPo = converter.toPo(dataTransferLogRequestDto);
        if (dataTransferLogRepository.add(dataTransferLogPo) != 1) {
            throw new RuntimeException("新增数据迁移记录失败");
        }
        return dataTransferLogPo.getPkId();
    }

    /*
     * @Method: modifyById <br>
     * @Param: [dataTransferLogRequestDto] <br>
     * @Return: void 根据主键更新<br>
     * @Description：<br>
     * @Author： wz.li<br>
     * @Date： 2023/8/18 13:51 <br>
     * @Version： V2.0.2<br>
     */
    @Override
    public void modifyById(DataTransferLogRequestDto dataTransferLogRequestDto) {
        if (dataTransferLogRepository.modifyById(converter.toPo(dataTransferLogRequestDto)) != 1) {
            throw new RuntimeException("更新数据迁移记录失败");
        }
    }

    @Override
    @Transactional
    public ResponseEntity<String> addDownNumLog(List<DataTransferDownwardNumDto> dataTransferDownwardNumDtoList) {
        if (dataTransferDownwardNumDtoList.isEmpty()){
            return new ResponseEntity<>("No find Task",HttpStatus.OK);
        }
        for (DataTransferDownwardNumDto dataTransferDownwardNumDto: dataTransferDownwardNumDtoList) {
            List<DownNumInfo> downNumInfoList = dataTransferDownwardNumDto.getDownNumInfo();
            if (!downNumInfoList.isEmpty()) {
                for (DownNumInfo downNumInfo : downNumInfoList) {
                    DataTransferLogRequestDto dataTransferLogRequestDto = converter.toDownNumDto(dataTransferDownwardNumDto, downNumInfo);
                    dataTransferLogRequestDto.setStatus("SUCCESS");
                    dataTransferLogRequestDto.setPkId(null);
                    if (dataTransferLogRepository.add(converter.toPo(dataTransferLogRequestDto)) != 1) {
                        throw new RuntimeException("logTask Fail");
                    }
                    dataTransferTaskRepository.modifySuccessNum(dataTransferDownwardNumDto.getTaskId());
                    dataTransferTaskRepository.modifyStatus(DateUtil.now(),dataTransferDownwardNumDto.getTaskId());
                }
            }
        }
        return new ResponseEntity<>("success", HttpStatus.OK);
    }
}
